package com.koala.learn.reflectlearn;

import cn.hutool.core.util.ReflectUtil;
import com.alibaba.excel.annotation.ExcelProperty;
import com.google.common.collect.Lists;
import lombok.Data;
import org.junit.Test;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * @author yantingrui
 * @date 2022/8/3   15:39
 */
@Data
public class ClassProperty {
    @ExcelProperty("图片1")
    private String pic1;
    @ExcelProperty("图片2")
    private String pic2;
    @ExcelProperty("图片3")
    private String pic3;
    @ExcelProperty("图片4")
    private String pic4;
    @ExcelProperty("图片5")
    private String pic5;

    public void setPic1(String pic1) {
        this.pic1 = pic1;
    }

    /**
     *
     */
    @Test
    public void test27() throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        //Method[] declaredMethods = this.getClass().getDeclaredMethods();
        //for(Method method : declaredMethods){
        //    System.out.println(method.getName());
        //}
        //System.out.println(JSON.toJSONString(declaredMethods));
        //this.getClass().getDeclaredMethod("setPic1");
        //this.getClass().getDeclaredMethod("setPic1").invoke(this,"123");
        //this.getClass().getDeclaredField("pic1").set(this,"123");
        //System.out.println(this.getPic1());


    }

    @Test
    public void setPic(List<String> pics) throws InvocationTargetException, IllegalAccessException {
        for (int i = 1, size = pics.size(); i <= size; i++) {
            Method method = ReflectUtil.getMethodByName(this.getClass(), String.format("setPic%s", i));
            method.invoke(this, pics.get(i - 1));
        }
    }


    /**
     *
     */
    @Test
    public void test67() throws InvocationTargetException, IllegalAccessException {
        ArrayList<String> strings = Lists.newArrayList("1", "2", "3", "4");
        setPic(strings);
        ClassProperty classProperty = new ClassProperty();
        classProperty.setPic(strings);
        System.out.println(classProperty.toString());
    }

    /**
     *
     */
    @Test
    public void test56() {
        Method method = ReflectUtil.getMethodByName(this.getClass(), "setPic1");
        //Method method = getSetMethod(this.getClass(), "pic1");
        try {
            method.invoke(this, "123");
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException("Reflect error!");
        }
        System.out.println(this.getPic1());
    }

    /**
     * java反射bean的set方法
     *
     * @param objectClass objectClass
     * @param fieldName   fieldName
     * @return Method
     * @throws RuntimeException
     */
    public static Method getSetMethod(Class<?> objectClass, String fieldName) {
        try {
            Class<?>[] parameterTypes = new Class<?>[1];
            Field field = objectClass.getDeclaredField(fieldName);
            parameterTypes[0] = field.getType();
            StringBuilder sb = new StringBuilder();
            sb.append("set");
            sb.append(fieldName.substring(0, 1).toUpperCase(Locale.ROOT));
            sb.append(fieldName.substring(1));
            return objectClass.getMethod(sb.toString(), parameterTypes);
        } catch (NoSuchFieldException | NoSuchMethodException e) {
            throw new RuntimeException("Reflect error!");
        }
    }


}
